package com.seed.core.builder.keyword;

import cn.hutool.core.util.StrUtil;
import com.seed.core.builder.pojo.Join1;
import com.seed.core.builder.pojo.Table1;
import com.seed.core.builder.pojo.WhereCondition1;
import com.seed.core.builder.query.InBuilder;
import com.seed.core.column.Column;
import com.seed.core.enumeration.PropertyType;
import com.seed.core.enumeration.SqlOp;
import com.seed.core.pojo.seed.HasManys;
import com.seed.core.pojo.seed.Relation;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.pojo.Declared;
import com.seed.util.Join1Kit;
import com.seed.util.SeedKit;
import com.seed.util.StrKit;

import java.util.*;

public class KeywordWrapper {

    private final List<WhereCondition1> wheres = new ArrayList<>(); // 关键字条件对象
    private final Map<String, Join1> joins = new LinkedHashMap<>(); // 表关联对象
    private final Map<String, InBuilder> inBuilders = new LinkedHashMap<>();
    private final Map<String, Declared> fillKeywordeds = new HashMap<>(); // fill对象使用
    private String keyword;

    public KeywordWrapper(Seed selfSeed, Map<String, Declared> declared, String keyword) {
        if(StrUtil.isNotBlank(keyword)) {
            this.keyword = keyword;
            init(null, selfSeed, declared);
        }
    }

    private void init(String basePath, Seed selfSeed, Map<String, Declared> declared) {
        String selfTableAlias = StrKit.selfTableAlias(basePath);
//        QueryTable selfTable = new QueryTable(selfSeed.getTableName()).as(selfTableAlias);
        Table1 selfTable = new Table1(selfSeed.getTableName()).as(selfTableAlias);
        for(Map.Entry<String, Declared> entry: declared.entrySet()) {
            String crossOrColumn = entry.getKey();
            String finalPath = StrKit.concat(basePath, crossOrColumn);
            Declared value = entry.getValue();
            PropertyType propertyType = value.getType();
            switch (propertyType) {
                case none -> {
                    Column nameColumn = SeedKit.getNameColumn(selfSeed);
//                    SqlOp op = SeedKit.op(selfSeed, nameColumn);
//                    wheres.add(ConditionUtil.condition(selfTable, nameColumn, op.name(), keyword));
                    wheres.add(new WhereCondition1(selfTable, nameColumn.getName(), nameColumn.getDefaultOp(), keyword));
                }
                case asterisk -> {
                    Map<String, Column> columns = selfSeed.getColumns();
                    for (Column column: columns.values()) {
                        String columName = column.getName();
//                        SqlOp op = SeedKit.op(selfSeed, columName);
//                        wheres.add(ConditionUtil.condition(selfTable, columName, op.name(), keyword));
                        wheres.add(new WhereCondition1(selfTable, columName, column.getDefaultOp(), keyword));
                    }
                }
                case column -> {
//                    SqlOp op = SeedKit.op(selfSeed, crossOrColumn);
//                    wheres.add(ConditionUtil.condition(selfTable, crossOrColumn, op.name(), keyword));
                    Column column = (Column) value.getProperty();
                    wheres.add(new WhereCondition1(selfTable, crossOrColumn, column.getDefaultOp(), keyword));
                }
                case hasOne, belongsTo -> {
                    Relation relation = (Relation) value.getProperty();
                    Join1 join1 = Join1Kit.buildJoin1(relation, basePath, crossOrColumn);
//                    Seed targetSeed = relation.targetSeed();
//                    String targetTableAlias = StrUtil.toCamelCase(finalPath);
//                    QueryTable targetTable = new QueryTable(targetSeed.getTableName()).as(targetTableAlias);
//
//                    QueryCondition on = relation.getQueryCondition(basePath, crossOrColumn);
                    joins.putIfAbsent(finalPath, join1);
                    init(finalPath, relation.targetSeed(), value);
                }
                default -> {
                    Relation relation = (Relation) value.getProperty();
                    Seed targetSeed = relation.targetSeed();
                    inBuilders.put(finalPath, new InBuilder(selfSeed, basePath, crossOrColumn, (HasManys) relation, targetSeed, keyword, value));
                    fillKeywordeds.put(finalPath, value);
                }
            }
        }
    }

    public String getKeyword() {
        return keyword;
    }

    public List<WhereCondition1> getWheres() {
        return wheres;
    }

    public Map<String, Join1> getJoins() {
        return joins;
    }

    public Map<String, InBuilder> getInBuilders() {
        return inBuilders;
    }

    public Declared getFillKeyworded(String key) {
        return fillKeywordeds.get(key);
    }
}
